# Maintainer: wirx6 <wirx654@gmail.com>

_realname=aom
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.1
pkgrel=1
pkgdesc='AV1 codec library(mingw-w64)'
arch=('any')
url="https://aomedia.org/"
license=('BSD')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("perl"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-yasm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://storage.googleapis.com/aom-releases/libaom-${pkgver}.tar.gz"{,.asc}
        "fix-lib-dest.patch")
sha256sums=('a0cff299621e2ef885aba219c498fa39a7d9a7ddf47585a118fd66c64ad1b312'
            'SKIP'
            '3a202005676f91b2b5ee5d7c10792f1930b204f36a96d7d5b893ef81d375b19d')
validpgpkeys=('B002F08B74A148DAA01F7123A48E86DB0B830498')
noextract=("libaom-${pkgver}.tar.gz")

prepare() {
  # Extract into a subdirectory
  (mkdir "${srcdir}/${_realname}" && cd "${srcdir}/${_realname}" && bsdtar -xf "${srcdir}/libaom-${pkgver}.tar.gz")

  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}"/fix-lib-dest.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=1 \
    -DENABLE_TESTS=OFF \
    ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${CARCH}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 ${srcdir}/${_realname}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/${_realname}/PATENTS ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/PATENTS
}
